package com.bit.strongbaby;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Paint.Align;
import android.graphics.Paint.Cap;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.widget.FrameLayout;

/**
 * GraphView creates a scaled line or bar graph with x and y axis labels.
 * 
 * @author Arno den Hond
 * 
 */
public class GraphActivity extends View {
	public GraphActivity(Context context) {
		super(context);
	}

	public GraphActivity(Context context, AttributeSet attrs) {
		super(context, attrs);
	}

	public GraphActivity(Context context, AttributeSet attrs, int defStyleAttr) {
		super(context, attrs, defStyleAttr);
	}

	public static boolean BAR = true;
	public static boolean LINE = false;

	public boolean isInit = false;

	private Paint paint;
	private float[] valmah;
	private float[] valwah;
	private float[] valbh;
	private String[] valbt;
	private String[] horlabels;
	private String[] verlabels;
	private String title;
	private boolean type;

	int[] a;

	public void initGraph(float[] valuemah, float[] valuewah, float[] valuebh,
			String[] valuebt, String title, String[] horlabels,
			String[] verlabels, boolean type) {
		if (valuemah == null)
			valuemah = new float[0];
		else
			this.valmah = valuemah;
		if (valuewah == null)
			valuewah = new float[0];
		else
			this.valwah = valuewah;
		if (valuebh == null)
			valuebh = new float[0];
		else
			this.valbh = valuebh;
		if (valuebt == null)
			valuebt = new String[0];
		else
			this.valbt = valuebt;
		if (title == null)
			title = "";
		else
			this.title = title;
		if (horlabels == null)
			this.horlabels = new String[0];
		else
			this.horlabels = horlabels;
		if (verlabels == null)
			this.verlabels = new String[0];
		else
			this.verlabels = verlabels;
		this.type = type;
		paint = new Paint();

		isInit = true;
		invalidate();
	}

	@Override
	protected void onDraw(Canvas canvas) {
		Log.d("욧욧0", valbt[0] + "");
		Log.d("욧욧1", valbt[1] + "");
		Log.d("욧욧2", valbt[2] + "");
		Log.d("욧욧3", valbt[3] + "");
		Log.d("욧욧4", valbt[4] + "");

		if (isInit == false)
			return;

		float border = 250;
		float borders = 40;
		float horstart = (border / 5);
		float height = getHeight();
		float width = getWidth() - 40;
		float max = 80;
		float min = 50;
		float diff = max - min;
		float graphheight = height - border - 40;
		float graphwidth = width - (2 / border) - 50;

		// Paint pnt = new Paint(Paint.ANTI_ALIAS_FLAG);
		// Bitmap draw_bitmap =
		// Bitmap.createBitmap(700,700,Bitmap.Config.ARGB_8888);
		// canvas.scale((float)0.5, (float)0.5);
		// canvas.drawBitmap(draw_bitmap, 0, 0,pnt);

		paint.setTextAlign(Align.LEFT);
		int vers = verlabels.length - 1;
		for (int i = 0; i < verlabels.length; i++) {
			paint.setColor(Color.LTGRAY);
			paint.setAlpha(70); // 투명도
			paint.setStrokeWidth(5.0F); // 선굵기
			float y = ((graphheight / vers) * i) + border;
			canvas.drawLine(horstart, y, width, y, paint);
			paint.setColor(Color.BLACK); // cm글자 색
			paint.setTextSize(25);
			canvas.drawText(verlabels[i], 0, y, paint);
		}
		int hors = horlabels.length - 1;
		for (int i = 0; i < horlabels.length; i++) {
			paint.setColor(Color.LTGRAY);
			paint.setAlpha(70); // 투명도
			paint.setStrokeWidth(5.0F); // 선굵기
			float x = ((graphwidth / hors) * i) + horstart;
			canvas.drawLine(x, height - (border / 10), x, border, paint);
			paint.setTextAlign(Align.CENTER);
			if (i == horlabels.length - 1)
				paint.setTextAlign(Align.CENTER);
			if (i == 0)
				paint.setTextAlign(Align.CENTER);
			paint.setColor(Color.BLACK); // 개월수
			paint.setTextSize(25);
			canvas.drawText(horlabels[i], x, height - 4, paint);
		}

		paint.setTextAlign(Align.CENTER);
		paint.setColor(Color.BLUE); // title
		paint.setTextSize(100);
		canvas.drawText(title, (graphwidth / 2) + horstart, border - 4, paint);

		if (max != min) {

			if (type == BAR) {
				float datalength = valmah.length;
				float colwidth = (width - (2 * border)) / datalength;
				for (int i = 0; i < valmah.length; i++) {
					float val = valmah[i] - min;
					float rat = val / diff;
					float h = graphheight * rat;
					canvas.drawRect((i * colwidth) + horstart, (border - h)
							+ graphheight, ((i * colwidth) + horstart)
							+ (colwidth - 1), height - (border - 1), paint);

				}
				// 꺽은선 그래프
			} else {
				// 남자아이 키
				float datalengthmah = valmah.length;
				float colwidthmah = (width - (1.25f * borders)) / datalengthmah;
				float halfcolmah = colwidthmah;
				float lasthmah = 0;
				paint.setColor(Color.parseColor("#7da3d6"));
				for (int i = 0; i < valmah.length; i++) {
					float vvalmah = valmah[i] - min;
					float ratmah = vvalmah / diff;
					float hmah = graphheight * ratmah;
					if (i % 2 == 1) {
						canvas.drawLine(((i - 1) * colwidthmah)
								+ (horstart + 1) + halfcolmah,
								(border - lasthmah) + graphheight,
								(i * colwidthmah) + (horstart + 1)
										+ (halfcolmah), (border - hmah)
										+ graphheight, paint);
						paint.setStrokeCap(Cap.ROUND);
					}
					if (i % 2 == 0) {
						canvas.drawLine(((i - 1) * colwidthmah)
								+ (horstart + 1) + halfcolmah,
								(border - lasthmah) + graphheight,
								(i * colwidthmah) + (horstart + 1)
										+ (halfcolmah), (border - hmah)
										+ graphheight, paint);
					}
					lasthmah = hmah;
				}
				// 여자아이 키
				float datalengthwah = valwah.length;
				float colwidthwah = (width - (1.25f * borders)) / datalengthwah;
				float halfcolwah = colwidthwah;
				float lasthwah = 0;
				paint.setColor(Color.parseColor("#ce5a5a"));
				for (int j = 0; j < valwah.length; j++) {
					float vvalwah = valwah[j] - min;
					float ratwah = vvalwah / diff;
					float hwah = graphheight * ratwah;
					if (j % 2 == 1) {
						canvas.drawLine(((j - 1) * colwidthwah)
								+ (horstart + 1) + halfcolwah,
								(border - lasthwah) + graphheight,
								(j * colwidthwah) + (horstart + 1)
										+ (halfcolwah), (border - hwah)
										+ graphheight, paint);
					}
					if (j % 2 == 0) {
						canvas.drawLine(((j - 1) * colwidthwah)
								+ (horstart + 1) + halfcolwah,
								(border - lasthwah) + graphheight,
								(j * colwidthwah) + (horstart + 1)
										+ (halfcolwah), (border - hwah)
										+ graphheight, paint);
					}
					// canvas.drawLine(startX, startY, stopX, stopY, paint);
					lasthwah = hwah;
				}
				// 입력값 점
				float datalengtbh = valbh.length;
				float colwidtbh = (width - (1.25f * borders)) / datalengtbh;
				float halfcolbh = colwidtbh;
				float lasthbh = 0;
				paint.setColor(Color.GRAY);

				for (int j = 0; j < valbh.length; j++) {
					float vvalbh = valbh[j] - min;
					float ratbh = vvalbh / diff;
					float hbh = graphheight * ratbh;

					if (j % 2 == 1) {
						canvas.drawPoint(((j - 1) * colwidtbh) + (horstart + 1)
								+ halfcolbh, (border - lasthbh) + graphheight,
								paint);
						canvas.drawText(Integer.toString((int)valbh[j])+"cm", ((j) * colwidtbh)
								+ (horstart + 1) + halfcolbh,
								(border - hbh) + graphheight - 50, paint);
						paint.setStrokeWidth(20f);
						canvas.drawText(valbt[j], ((j) * colwidtbh)
								+ (horstart + 1) + halfcolbh,
								(border - hbh) + graphheight - 20, paint);
						paint.setTextSize(25);
//						Log.d("데이트", valbt[j] + "");
//						Log.d("키", a[j] + "");
					}
//					Log.d("데이트0", valbt[0] + "");
//					Log.d("데이트1", valbt[1] + "");
//					Log.d("데이트2", valbt[2] + "");
//					Log.d("데이트3", valbt[3] + "");
//					Log.d("데이4", valbt[4] + "");

					if (j % 2 == 0) {
						canvas.drawPoint(((j - 1) * colwidtbh) + (horstart + 1)
								+ halfcolbh, (border - lasthbh) + graphheight,
								paint);
						canvas.drawText(Integer.toString((int)valbh[j])+"cm", ((j) * colwidtbh)
								+ (horstart + 1) + halfcolbh,
								(border - hbh) + graphheight - 50, paint);
						paint.setStrokeWidth(20f);
						canvas.drawText(valbt[j], ((j) * colwidtbh)
								+ (horstart + 1) + halfcolbh,
								(border - hbh) + graphheight - 20, paint);
						paint.setTextSize(25);
					}
					// canvas.drawLine(startX, startY, stopX, stopY, paint);
					lasthbh = hbh;
				}
			}
		}

	}

	private float getMax() {
		float largest = Integer.MIN_VALUE;
		for (int i = 0; i < valmah.length; i++)
			if (valmah[i] > largest)
				largest = valmah[i];
		return largest;
	}

	private float getMin() {
		float smallest = Integer.MAX_VALUE;
		for (int i = 0; i < valmah.length; i++)
			if (valmah[i] < smallest)
				smallest = valmah[i];
		return smallest;
	}

	public void resizeCompass() {
		int width = this.getWidth();
		int height = this.getHeight();

		FrameLayout.LayoutParams lp = null;

		if (width > 150) {
			lp = new FrameLayout.LayoutParams(150, 150);
		} else {
			lp = new FrameLayout.LayoutParams(LayoutParams.MATCH_PARENT,
					LayoutParams.MATCH_PARENT);
		}

		this.setLayoutParams(lp);
	}

	public void resizeCompass(int width, int height) {

		// View 크기 조절
		FrameLayout.LayoutParams lp = null;
		// lp = new FrameLayout.LayoutParams(width, height);
		lp = (FrameLayout.LayoutParams) this.getLayoutParams();
		lp.width = 100;
		lp.height = 200;
		this.setLayoutParams(lp);
	}

}